<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nginx流量控制指南 | 限制请求速率与连接数</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.6;
            color: #333;
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .code-block {
            background-color: #2d3748;
            border-left: 4px solid #667eea;
        }
        .highlight {
            background: linear-gradient(120deg, rgba(253, 230, 138, 0.3) 0%, rgba(253, 230, 138, 0.3) 100%);
            background-repeat: no-repeat;
            background-size: 100% 35%;
            background-position: 0 88%;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 max-w-4xl">
            <div class="flex flex-col items-center text-center">
                <span class="inline-block bg-white text-purple-700 text-xs font-semibold px-3 py-1 rounded-full mb-4">NGINX优化</span>
                <h1 class="text-4xl md:text-5xl font-bold leading-tight mb-6">Nginx流量控制完全指南</h1>
                <p class="text-xl text-purple-100 max-w-2xl">掌握限制请求速率与连接数的核心技巧，保护服务器免受恶意流量冲击</p>
                <div class="mt-8 flex space-x-4">
                    <span class="inline-flex items-center px-3 py-1 bg-white bg-opacity-20 rounded-full text-sm">
                        <i class="fas fa-shield-alt mr-2"></i>安全防护
                    </span>
                    <span class="inline-flex items-center px-3 py-1 bg-white bg-opacity-20 rounded-full text-sm">
                        <i class="fas fa-tachometer-alt mr-2"></i>性能优化
                    </span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12 max-w-4xl">
        <!-- Introduction -->
        <section class="mb-16">
            <p class="text-lg text-gray-700 mb-6 leading-relaxed">
                在Nginx中，限制请求速率和连接数是用于控制流量和保护服务器资源的常见功能。这些限制可以帮助防止恶意攻击、资源滥用以及保持服务器性能稳定。Nginx提供了多种方法来实现这些限制，主要包括<code class="bg-gray-100 px-2 py-1 rounded">limit_req</code>模块和<code class="bg-gray-100 px-2 py-1 rounded">limit_conn</code>模块。
            </p>
            
            <div class="bg-blue-50 border-l-4 border-blue-500 p-4 my-8 rounded">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <i class="fas fa-info-circle text-blue-500 text-xl"></i>
                    </div>
                    <div class="ml-3">
                        <p class="text-sm text-blue-700">
                            这些技术对于防止DDoS攻击、API滥用和资源耗尽特别有效，是生产环境中必备的配置。
                        </p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Rate Limiting Section -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <h2 class="text-3xl font-bold text-gray-800 flex items-center">
                    <span class="bg-blue-100 text-blue-700 w-10 h-10 rounded-full flex items-center justify-center mr-4">1</span>
                    限制请求速率 (Rate Limiting)
                </h2>
            </div>

            <p class="text-gray-700 mb-6">
                限制请求速率的功能可以用来控制每个客户端在单位时间内发送的请求数量。这通常用于防止DDoS攻击或滥用。
            </p>

            <!-- Subsection -->
            <div class="mb-12">
                <h3 class="text-2xl font-semibold text-gray-800 mb-4 flex items-center">
                    <i class="fas fa-cog text-blue-500 mr-3"></i>
                    limit_req 模块
                </h3>
                
                <p class="text-gray-700 mb-6">
                    <code class="bg-gray-100 px-2 py-1 rounded">limit_req</code>模块是Nginx中用于限制请求速率的主要模块。其基本配置包括创建一个"限制区域"，指定允许的请求速率，并应用到具体的<code class="bg-gray-100 px-2 py-1 rounded">location</code>或<code class="bg-gray-100 px-2 py-1 rounded">server</code>块中。
                </p>

                <!-- Configuration Steps -->
                <div class="mb-8">
                    <h4 class="text-xl font-medium text-gray-800 mb-4 flex items-center">
                        <span class="bg-gray-200 text-gray-700 w-6 h-6 rounded-full flex items-center justify-center text-sm mr-3">1</span>
                        配置步骤
                    </h4>

                    <div class="grid md:grid-cols-2 gap-6">
                        <!-- Step 1 -->
                        <div class="bg-white rounded-lg shadow-md p-6 card-hover transition-all duration-300">
                            <div class="flex items-center mb-3">
                                <div class="bg-blue-100 p-2 rounded-full mr-3">
                                    <i class="fas fa-map-marked-alt text-blue-500"></i>
                                </div>
                                <h5 class="font-semibold">定义限制区域</h5>
                            </div>
                            <p class="text-sm text-gray-600 mb-4">
                                使用<code>limit_req_zone</code>指令定义请求速率的限制区域。
                            </p>
                            <div class="code-block text-gray-200 p-4 rounded text-sm overflow-x-auto">
                                <pre>limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s;</pre>
                            </div>
                            <div class="mt-3 text-xs text-gray-500">
                                <i class="fas fa-info-circle mr-1"></i>
                                参数说明：<br>
                                <span class="ml-4">- $binary_remote_addr: 客户端IP地址</span><br>
                                <span class="ml-4">- mylimit: 区域名称</span><br>
                                <span class="ml-4">- 10m: 内存大小</span><br>
                                <span class="ml-4">- 1r/s: 每秒请求数</span>
                            </div>
                        </div>

                        <!-- Step 2 -->
                        <div class="bg-white rounded-lg shadow-md p-6 card-hover transition-all duration-300">
                            <div class="flex items-center mb-3">
                                <div class="bg-blue-100 p-2 rounded-full mr-3">
                                    <i class="fas fa-code text-blue-500"></i>
                                </div>
                                <h5 class="font-semibold">应用限制</h5>
                            </div>
                            <p class="text-sm text-gray-600 mb-4">
                                使用<code>limit_req</code>指令将限制应用到具体位置。
                            </p>
                            <div class="code-block text-gray-200 p-4 rounded text-sm overflow-x-auto">
                                <pre>location /api/ {
    limit_req zone=mylimit burst=5 nodelay;
    proxy_pass http://backend;
}</pre>
                            </div>
                            <div class="mt-3 text-xs text-gray-500">
                                <i class="fas fa-info-circle mr-1"></i>
                                参数说明：<br>
                                <span class="ml-4">- burst: 允许的突发请求数</span><br>
                                <span class="ml-4">- nodelay: 不延迟处理突发请求</span>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Example -->
                <div>
                    <h4 class="text-xl font-medium text-gray-800 mb-4 flex items-center">
                        <i class="fas fa-code-branch text-blue-500 mr-3"></i>
                        完整配置示例
                    </h4>
                    <div class="bg-white rounded-lg shadow-md overflow-hidden">
                        <div class="code-block text-gray-200 p-4 text-sm overflow-x-auto">
                            <pre>http {
    limit_req_zone $binary_remote_addr zone=mylimit:10m rate=5r/m;

    server {
        listen 80;

        location / {
            limit_req zone=mylimit burst=10;
            proxy_pass http://backend;
        }
    }
}</pre>
                        </div>
                        <div class="bg-gray-800 text-gray-300 px-4 py-2 text-xs">
                            <i class="fas fa-lightbulb mr-2"></i>这个配置每分钟允许每个IP地址最多5次请求，并允许最多10次的突发请求。
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Connection Limiting Section -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <h2 class="text-3xl font-bold text-gray-800 flex items-center">
                    <span class="bg-purple-100 text-purple-700 w-10 h-10 rounded-full flex items-center justify-center mr-4">2</span>
                    限制连接数 (Connection Limiting)
                </h2>
            </div>

            <p class="text-gray-700 mb-6">
                限制连接数的功能用于控制每个客户端与服务器建立的连接数量。这有助于避免某些客户端占用过多连接资源，从而影响其他客户端的正常访问。
            </p>

            <!-- Subsection -->
            <div class="mb-12">
                <h3 class="text-2xl font-semibold text-gray-800 mb-4 flex items-center">
                    <i class="fas fa-plug text-purple-500 mr-3"></i>
                    limit_conn 模块
                </h3>
                
                <p class="text-gray-700 mb-6">
                    <code class="bg-gray-100 px-2 py-1 rounded">limit_conn</code>模块用于限制每个IP地址的连接数量。它的基本配置包括创建一个"连接限制区域"，指定每个区域允许的最大连接数，并将其应用到具体的<code class="bg-gray-100 px-2 py-1 rounded">location</code>或<code class="bg-gray-100 px-2 py-1 rounded">server</code>块中。
                </p>

                <!-- Configuration Steps -->
                <div class="mb-8">
                    <h4 class="text-xl font-medium text-gray-800 mb-4 flex items-center">
                        <span class="bg-gray-200 text-gray-700 w-6 h-6 rounded-full flex items-center justify-center text-sm mr-3">1</span>
                        配置步骤
                    </h4>

                    <div class="grid md:grid-cols-2 gap-6">
                        <!-- Step 1 -->
                        <div class="bg-white rounded-lg shadow-md p-6 card-hover transition-all duration-300">
                            <div class="flex items-center mb-3">
                                <div class="bg-purple-100 p-2 rounded-full mr-3">
                                    <i class="fas fa-project-diagram text-purple-500"></i>
                                </div>
                                <h5 class="font-semibold">定义连接限制区域</h5>
                            </div>
                            <p class="text-sm text-gray-600 mb-4">
                                使用<code>limit_conn_zone</code>指令定义连接限制区域。
                            </p>
                            <div class="code-block text-gray-200 p-4 rounded text-sm overflow-x-auto">
                                <pre>limit_conn_zone $binary_remote_addr zone=addr:10m;</pre>
                            </div>
                            <div class="mt-3 text-xs text-gray-500">
                                <i class="fas fa-info-circle mr-1"></i>
                                参数说明：<br>
                                <span class="ml-4">- $binary_remote_addr: 客户端IP地址</span><br>
                                <span class="ml-4">- addr: 区域名称</span><br>
                                <span class="ml-4">- 10m: 内存大小</span>
                            </div>
                        </div>

                        <!-- Step 2 -->
                        <div class="bg-white rounded-lg shadow-md p-6 card-hover transition-all duration-300">
                            <div class="flex items-center mb-3">
                                <div class="bg-purple-100 p-2 rounded-full mr-3">
                                    <i class="fas fa-network-wired text-purple-500"></i>
                                </div>
                                <h5 class="font-semibold">应用限制</h5>
                            </div>
                            <p class="text-sm text-gray-600 mb-4">
                                使用<code>limit_conn</code>指令将限制应用到具体位置。
                            </p>
                            <div class="code-block text-gray-200 p-4 rounded text-sm overflow-x-auto">
                                <pre>location / {
    limit_conn addr 10;
    proxy_pass http://backend;
}</pre>
                            </div>
                            <div class="mt-3 text-xs text-gray-500">
                                <i class="fas fa-info-circle mr-1"></i>
                                参数说明：<br>
                                <span class="ml-4">- addr: 连接限制区域的名称</span><br>
                                <span class="ml-4">- 10: 允许的最大连接数</span>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Example -->
                <div>
                    <h4 class="text-xl font-medium text-gray-800 mb-4 flex items-center">
                        <i class="fas fa-laptop-code text-purple-500 mr-3"></i>
                        完整配置示例
                    </h4>
                    <div class="bg-white rounded-lg shadow-md overflow-hidden">
                        <div class="code-block text-gray-200 p-4 text-sm overflow-x-auto">
                            <pre>http {
    limit_conn_zone $binary_remote_addr zone=addr:10m;

    server {
        listen 80;

        location / {
            limit_conn addr 5;
            proxy_pass http://backend;
        }
    }
}</pre>
                        </div>
                        <div class="bg-gray-800 text-gray-300 px-4 py-2 text-xs">
                            <i class="fas fa-lightbulb mr-2"></i>这个配置限制每个IP地址在同一时间最多只能有5个连接。
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Visualization -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold text-gray-800 mb-8 flex items-center">
                <i class="fas fa-project-diagram text-indigo-500 mr-4"></i>
                Nginx流量控制机制图解
            </h2>
            
            <div class="bg-white rounded-xl shadow-md p-6">
                <div class="mermaid">
                    graph TD
                    A[客户端请求] --> B{请求类型?}
                    B -->|普通请求| C[limit_req处理]
                    B -->|连接请求| D[limit_conn处理]
                    C --> E{速率检查}
                    D --> F{连接数检查}
                    E -->|在限制内| G[允许访问]
                    E -->|超出限制| H[返回503错误]
                    F -->|在限制内| G
                    F -->|超出限制| H
                    G --> I[后端服务]
                </div>
            </div>
        </section>

        <!-- Summary -->
        <section class="bg-indigo-50 rounded-xl p-8 mb-16">
            <h2 class="text-2xl font-bold text-indigo-800 mb-6 flex items-center">
                <i class="fas fa-graduation-cap text-indigo-600 mr-3"></i>
                关键要点总结
            </h2>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white p-6 rounded-lg shadow-sm">
                    <h3 class="font-semibold text-lg text-gray-800 mb-3 flex items-center">
                        <span class="bg-blue-100 text-blue-600 w-6 h-6 rounded-full flex items-center justify-center mr-3 text-sm">1</span>
                        请求速率限制
                    </h3>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-blue-500 mt-1 mr-2 text-sm"></i>
                            <span>使用<code class="bg-gray-100 px-1">limit_req_zone</code>定义限制区域</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-blue-500 mt-1 mr-2 text-sm"></i>
                            <span>通过<code class="bg-gray-100 px-1">rate</code>参数设置速率(如1r/s)</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-blue-500 mt-1 mr-2 text-sm"></i>
                            <span><code class="bg-gray-100 px-1">burst</code>参数控制突发请求处理</span>
                        </li>
                    </ul>
                </div>
                
                <div class="bg-white p-6 rounded-lg shadow-sm">
                    <h3 class="font-semibold text-lg text-gray-800 mb-3 flex items-center">
                        <span class="bg-purple-100 text-purple-600 w-6 h-6 rounded-full flex items-center justify-center mr-3 text-sm">2</span>
                        连接数限制
                    </h3>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-500 mt-1 mr-2 text-sm"></i>
                            <span>使用<code class="bg-gray-100 px-1">limit_conn_zone</code>定义限制区域</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-500 mt-1 mr-2 text-sm"></i>
                            <span>通过<code class="bg-gray-100 px-1">limit_conn</code>设置最大连接数</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-500 mt-1 mr-2 text-sm"></i>
                            <span>适用于防止单个客户端占用过多资源</span>
                        </li>
                    </ul>
                </div>
            </div>
            
            <div class="mt-8 bg-white p-6 rounded-lg shadow-sm">
                <h3 class="font-semibold text-lg text-gray-800 mb-3 flex items-center">
                    <i class="fas fa-exclamation-triangle text-yellow-500 mr-3"></i>
                    最佳实践建议
                </h3>
                <div class="grid md:grid-cols-2 gap-4">
                    <div class="flex items-start">
                        <div class="bg-yellow-100 text-yellow-600 p-2 rounded-full mr-3 flex-shrink-0">
                            <i class="fas fa-balance-scale text-xs"></i>
                        </div>
                        <p class="text-gray-700 text-sm">根据业务需求合理设置限制值，避免过于严格影响正常用户</p>
                    </div>
                    <div class="flex items-start">
                        <div class="bg-yellow-100 text-yellow-600 p-2 rounded-full mr-3 flex-shrink-0">
                            <i class="fas fa-robot text-xs"></i>
                        </div>
                        <p class="text-gray-700 text-sm">结合日志分析调整参数，识别恶意流量模式</p>
                    </div>
                    <div class="flex items-start">
                        <div class="bg-yellow-100 text-yellow-600 p-2 rounded-full mr-3 flex-shrink-0">
                            <i class="fas fa-layer-group text-xs"></i>
                        </div>
                        <p class="text-gray-700 text-sm">多层次防护：结合防火墙、CDN等其他安全措施</p>
                    </div>
                    <div class="flex items-start">
                        <div class="bg-yellow-100 text-yellow-600 p-2 rounded-full mr-3 flex-shrink-0">
                            <i class="fas fa-tachometer-alt text-xs"></i>
                        </div>
                        <p class="text-gray-700 text-sm">监控系统资源使用情况，及时调整配置</p>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-400 py-8">
        <div class="container mx-auto px-6 max-w-4xl">
            <div class="flex flex-col items-center">
                <div class="text-sm mb-2">
                    <span class="font-medium text-gray-300">技术小馆</span>
                </div>
                <div class="text-xs">
                    <a href="http://www.yuque.com/jtostring" class="hover:text-white transition-colors duration-200">
                        <i class="fas fa-external-link-alt mr-1"></i>http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            fontFamily: 'Noto Sans SC, sans-serif',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // 微交互效果
        document.querySelectorAll('a').forEach(link => {
            link.addEventListener('mouseenter', function() {
                this.classList.add('underline');
            });
            link.addEventListener('mouseleave', function() {
                this.classList.remove('underline');
            });
        });
        
        // 代码块复制功能
        document.querySelectorAll('.code-block').forEach(block => {
            block.addEventListener('click', function() {
                const range = document.createRange();
                range.selectNode(this.querySelector('pre'));
                window.getSelection().removeAllRanges();
                window.getSelection().addRange(range);
                document.execCommand('copy');
                window.getSelection().removeAllRanges();
                
                // 显示复制成功提示
                const originalText = this.querySelector('.fa-lightbulb').parentNode.textContent;
                const notification = this.querySelector('.fa-lightbulb').parentNode;
                notification.innerHTML = '<i class="fas fa-check-circle mr-2"></i>已复制到剪贴板';
                
                setTimeout(() => {
                    notification.innerHTML = `<i class="fas fa-lightbulb mr-2"></i>${originalText.replace(/<i class="fas fa-lightbulb mr-2"><\/i>/g, '')}`;
                }, 2000);
            });
        });
    </script>
</body>
</html>